﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThongTinNhanKhau.model.BO
{
    class TamVangBO
    {
        private GUI.TamVang f;
        private GUI.NhanKhau nhanKhau;
        model.DAO.TamVangDAO tamVangDAO = new DAO.TamVangDAO();

        public TamVangBO(GUI.TamVang tamVang, GUI.NhanKhau nhanKhau)
        {
            // TODO: Complete member initialization
            this.f = tamVang;
            this.nhanKhau = nhanKhau;
        }

        internal void loadDefault(string maNhanKhau)
        {
            DataTable dt = new DataTable();
            dt = tamVangDAO.loadDefault(maNhanKhau);

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    this.f.tbHoTen.Text = dr["HoTen"].ToString();
                    this.f.tbGioiTinh.Text = dr["GioiTinh"].ToString();
                }
            }
        }

        internal void loadTable(String maNhanKhau)
        {
            DataTable dt = tamVangDAO.loadTable(maNhanKhau);
            if (dt.Rows.Count != 0)
            {
                this.f.gridControlTamVang.DataSource = dt;
            }
            else
            {
                this.f.gridControlTamVang.DataSource = null;
            }
        }

        internal void genarateMaTamVang()
        {
            //genarate MaTamVang
            StringBuilder maTamVang = new StringBuilder();
            Random random = new Random();

            do
            {
                maTamVang.Clear();
                maTamVang.Append("MTV");
                for (int i = 0; i < 5; i++)
                {
                    maTamVang.Append(random.Next(0, 9));
                }
            } while (tamVangDAO.isValidMaTamVang(maTamVang.ToString()));

            this.f.tbMaTamVang.Text = maTamVang.ToString();
        }

        internal void btnLuu()
        {
            String maTamVang = this.f.tbMaTamVang.Text;
            String maNhanKhau = this.f.tbMaNhanKhau.Text;
            String ngayTamVang = this.f.dateTimeNgayTamVang.Value.ToString("yyyy-MM-dd");
            String lyDoTamVang = this.f.tbLyDoTamVang.Text;
            String tinhTrang;

            if ("Mở".Equals(this.f.cboTinhTrang.SelectedItem.ToString()))
            {
                tinhTrang = "Enable";
            }
            else
            {
                tinhTrang = "Disable";
            }

            //if Update
            if (tamVangDAO.isValidMaTamVang(maTamVang))
            {
                if (tamVangDAO.capNhatTamVang(maTamVang, maNhanKhau, ngayTamVang, lyDoTamVang, tinhTrang))
                {
                    System.Windows.Forms.MessageBox.Show("Cập nhật thành công");
                    loadTable(maNhanKhau);
                    nhanKhau.loadTable();
                }
            }
            else //if New
            {
                if (tamVangDAO.themMoiTamVang(maTamVang, maNhanKhau, ngayTamVang, lyDoTamVang, tinhTrang))
                {
                    System.Windows.Forms.MessageBox.Show("Thêm mới thành công");
                    loadTable(maNhanKhau);
                    nhanKhau.loadTable();
                }
            }
        }

        internal void deleteRow(string maTamVang, string maNhanKhau)
        {
            if (tamVangDAO.deleteRow(maTamVang, maNhanKhau))
            {
                System.Windows.Forms.MessageBox.Show("Xóa thành công");
                loadTable(maNhanKhau);
                nhanKhau.loadTable();
            }
        }
    }
}
